
/* 
1,修改service引入
*/

const {baseController} = require('../../baseController')
const {userPointScoreService} = require('../../../service/impl/userPointScoreService/userPointScoreService')
const { result } = require('../../../util/result/result')




class userPointScoreController {
    static sigleIns = new userPointScoreController()

    static getSigleIns =()=>{
        if(this.sigleIns){
            return this.sigleIns
        }else{
            return new userPointScoreController()
        }
    }

    constructor(){
        this.service =  userPointScoreService.getIns()
    }

    async create(req,res){
        let data 
        try {
            data =  await  this.service.create(req.body)
        } catch (error) {
            console.log(error);
            return
        }
        if(data==undefined){
            res.send(result(false))
        }else{
            res.send(result(true,data))
        }

    }

    async findByIdAndRemove(req,res){
        let data  
        try {
            data =  await  this.service.findByIdAndRemove(req.body)
        } catch (error) {
            // res.send(result(false,error))
        }

        if(data==undefined){
            res.send(result(false))
        }else{
            res.send(result(true,data))
        }



    }

    async findByIdAndUpdate(req,res){
        let data  
        let id = req.body.id
        let updateContent =req.body.updateContent
        try {
            data =  await  this.service.findByIdAndUpdate(id,updateContent)
        } catch (error) {
            console.log(error);
            // res.send(result(false,error))
        }

        if(data&&data!=undefined){
            res.send(result(true,data))
         }else{
            res.send(result(false))
         }

    }

    async queryAll(req,res){
        let data  
        try {
            data =  await  this.service.queryAll()
        } catch (error) {
                console.log(error)
            return 
        }
        if(data&&data!=undefined){
            res.send(result(true,data))
         }else{
            res.send(result(false))
         }
    }


    // andPopulate
    /* 
   {
    obj:{id:'ffds43434'},
    param1:'userid',
    param2:'professionid'
   }
    
    */
   async  findByObj(req,res){
    let data 
    let obj = req.body.obj
    let param1 = req.body.param1
    let param2 = req.body.param2


    try {
        data =  await  this.service.findAndPopulate(obj,param1,param2)
    } catch (error) {
        res.send(result(false,error))
        return
    }
    if(data&&data!=undefined){
        res.send(result(true,data))
     }else{
        res.send(result(false))
     }
   }

   /* 
   入参:
   id,查找的id
   param1,填充的key
   param2,填充的key
   示例
   {
    id:'dsdasda13',
    param1:'user',
    param2:'profession'
   }
   */
   async findByIdAndPopulate (req,res){
    let data
    let id = req.body.id
    let param1 = req.body.param1
    let param2 = req.body.param2
    try {
        data =  await  this.service.findByIdAndPopulate(id,param1,param2)
    } catch (error) {
        res.send(result(false,error))
        return
    }
    if(data&&data!=undefined){
        res.send(result(true,data))
     }else{
        res.send(result(false))
     }
   }



   /* 
   入参:
   req.body:{
       obj:{username:123},
       update_content:{
         
       }
   }
   */
   async findByObjAndUpdate (req,res){
    let data
    // 没有则创建,有则更新
    let options = {"upsert":true}


    try {
        data =  await  this.service.findByObjAndUpdate(req.body.obj,req.body.update_content,options)
    } catch (error) {
        res.send(result(false,error))
        return
    }
    if(data&&data!=undefined){
        res.send(result(true,data))
     }else{
        res.send(result(false))
     }
   }
}

module.exports={
    userPointScoreController
}